<template>
  <el-dialog
    :title="title"
    :visible.sync="dialogVisible"
    width="30%"
    :before-close="addDialogClose">
    <!-- 添加用户 -->
      <el-form :model="addRuleForm" :rules="addRules" ref="ruleForm" v-if="diaSonType === 'add'">
          <el-form-item label="用户名" prop="username">
            <el-input v-model="addRuleForm.username"></el-input>
          </el-form-item>
          <el-form-item label="密码" prop="password">
            <el-input v-model="addRuleForm.password"></el-input>
          </el-form-item>
          <el-form-item label="邮箱地址" prop="email">
            <el-input v-model="addRuleForm.email"></el-input>
          </el-form-item>
          <el-form-item label="手机号码" prop="mobile">
            <el-input v-model="addRuleForm.mobile"></el-input>
          </el-form-item>
        </el-form>
        <!-- 修改用户 -->
        <el-form label-width="70px" :model="diaEditForm" :rules="addRules" ref="editRuleForm" v-if="diaSonType === 'edit'">
            <el-form-item label="用户名">
              <el-input v-model="diaEditForm.username" disabled width="50px"></el-input>
            </el-form-item>
            <el-form-item label="邮箱" prop="email">
              <el-input v-model="diaEditForm.email"></el-input>
            </el-form-item>
            <el-form-item label="手机号" prop="mobile">
              <el-input v-model="diaEditForm.mobile"></el-input>
            </el-form-item>
        </el-form>
        <!-- 添加角色 -->
        <el-form :model="addRoles" :rules="roleRules" ref="roleForm" v-if="diaSonType === 'addroles'">
            <el-form-item label="角色名称" prop="roleName">
              <el-input v-model="addRoles.roleName"></el-input>
            </el-form-item>
            <el-form-item label="角色描述" prop="roleDesc">
              <el-input v-model="addRoles.roleDesc"></el-input>
            </el-form-item>
        </el-form>
         <!-- 编辑角色 -->
        <el-form :model="addRoles" :rules="roleRules" ref="roleForm" v-if="diaSonType === 'editroles'">
            <el-form-item label="角色名称" prop="roleName">
              <el-input v-model="addRoles.roleName"></el-input>
            </el-form-item>
            <el-form-item label="角色描述" prop="roleDesc">
              <el-input v-model="addRoles.roleDesc"></el-input>
            </el-form-item>
        </el-form>
        <!-- 分配角色 -->
        <div class="setUser" v-if="diaSonType === 'setUser'">
          <p>当前的用户: {{ userInfo.username }}</p>
          <br>
          <p>当前的角色：{{ userInfo.role_name }}</p>
          <br>
          <p>分配新角色：
             <el-select v-model="selectedRoleId" placeholder="请选择">
              <el-option
                v-for="item in roles"
                :key="item.id"
                :label="item.roleName"
                :value="item.id">
              </el-option>
            </el-select>
          </p>
        </div>
        <!-- 编辑分类的 -->
         <el-form :model="editRole" :rules="editCateRules" ref="editCateRef" v-if="diaSonType === 'editCate'">
            <el-form-item label="分类名称" prop="cat_name">
              <el-input v-model="editRole.cat_name"></el-input>
            </el-form-item>
        </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addDialogClose">取 消</el-button>
        <el-button type="primary" @click="addUser" v-if="diaSonType === 'add'">确 定</el-button>
        <el-button type="primary" @click="editUser" v-if="diaSonType === 'edit'">修 改</el-button>
        <el-button type="primary" @click="rolesPower" v-if="diaSonType === 'addroles'">确 定</el-button>
        <el-button type="primary" @click="editrolesPower" v-if="diaSonType === 'editroles'">修 改</el-button>
        <el-button type="primary" @click="setUser" v-if="diaSonType === 'setUser'">确 定</el-button>
        <el-button type="primary" @click="editCate" v-if="diaSonType === 'editCate'">确 定</el-button>
      </span>
  </el-dialog>
</template>

<script>
export default {
  props: ['dialogVisible', 'diaSonType', 'diaEditForm', 'addRoles', 'userInfo', 'roles', 'title', 'editRole'],
  data () {
    // 自定义手机验证规则
    var checkPhone = (rule, value, cb) => {
      const regPhone = /^(13[0-9]|14[01456879]|15[0-3,5-9]|16[2567]|17[0-8]|18[0-9]|19[0-3,5-9])\d{8}$/
      if (regPhone.test(value)) {
        return cb()
      }
      cb(new Error('请输入合法手机号'))
    }
    return {
      addRuleForm: {
        username: '',
        password: '',
        email: '',
        mobile: ''
      },
      addRules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 3, max: 10, message: '用户名长度在 3 到 10 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' },
          { min: 3, max: 15, message: '密码长度在 3 到 15 个字符', trigger: 'blur' }
        ],
        email: [
          { required: true, message: '请输入邮箱地址', trigger: 'blur' },
          { type: 'email', message: '请输入正确的邮箱地址', trigger: 'blur' }
        ],
        mobile: [
          {
            required: true,
            message: '请输入手机号',
            trigger: 'blur'
          },
          {
            validator: checkPhone,
            trigger: 'blur'
          }
        ]
      },
      roleRules: {
        roleName: [
          { required: true, message: '请输入角色名', trigger: 'blur' },
          { min: 2, max: 10, message: '角色名 2 到 10 个字符', trigger: 'blur' }
        ],
        roleDesc: [
          { required: true, message: '请输入角色描述', trigger: 'blur' },
          { min: 2, max: 10, message: '角色描述 2 到 10 个字符', trigger: 'blur' }
        ]
      },
      editCateRules: {
        cat_name: [
          { required: true, message: '请输入分类名称', trigger: 'blur' }
        ]
      },
      // 已选中角色的id值
      selectedRoleId: ''
    }
  },
  methods: {
    // 表单消失
    addDialogClose () {
      //  重置表单内容
      this.$emit('changeVisible')
      if (this.diaSonType === 'add') {
        this.$refs.ruleForm.resetFields()
      } else if (this.diaSonType === 'addroles') {
        this.$refs.roleForm.resetFields()
      } else if (this.diaSonType === 'setUser') {
        this.selectedRoleId = ''
        this.$emit('changeUseInfo')
      }
    },
    addUser () {
      // 对表单进行预校验
      this.$refs.ruleForm.validate(async vaild => {
        if (!vaild) return
        const { data: res } = await this.$http.post('users', this.addRuleForm)
        // console.log(res)
        if (res.meta.status !== 201) {
          return this.$message.error('添加失败')
        }
        this.$message.success('添加成功')
        this.$emit('setForm')
        this.$refs.ruleForm.resetFields()
      })
    },
    editUser () {
      // 对表单预校验
      this.$refs.editRuleForm.validate(vaild => {
        //  如果预校验失败直接 return
        if (!vaild) return
        this.$emit('editForm', this.diaEditForm)
      })
    },
    //  点击添加角色执行代码
    rolesPower () {
      // 对表单预校验
      this.$refs.roleForm.validate(vaild => {
        // 如果预校验失败直接 return
        if (!vaild) return
        this.$emit('sonRoles', this.addRoles)
        this.$refs.roleForm.resetFields()
      })
    },
    // 点击修改角色执行代码
    editrolesPower () {
      // 对表单预校验
      this.$refs.roleForm.validate(vaild => {
        // 如果预检验失败直接 return
        if (!vaild) return
        this.$emit('editSonRoles', this.addRoles)
      })
    },
    // 点击分配角色确定按钮执行代码
    setUser () {
      // console.log(this.selectedRoleId)
      if (!this.selectedRoleId) return this.$message.error('请选择需要设置的角色')
      this.$emit('setUserRole', this.selectedRoleId)
      this.selectedRoleId = ''
      this.$emit('changeUseInfo')
    },
    // 点击编辑角色后执行的代码
    editCate () {
      this.$refs.editCateRef.validate(valid => {
        if (!valid) return
        this.$emit('editSonCate', { name: this.editRole.cat_name, id: this.editRole.cat_id })
      })
    }
  }
}
</script>

<style>

</style>
